package com.ymatou.operate.trading.api.testcase;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.operate.trading.api.parameter.PayCompletedNotifyBean;
import com.ymatou.operate.trading.api.service.PayCompletedNotifyCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.database.sqlwapper.YmtTradingItemWapper;
import com.ymttest.utils.constant.ECode;

/**
 * 第三方交易完成通知 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_PayCompletedNotify {

	private static PayCompletedNotifyBean paycompletednotifybean;
	private static PayCompletedNotifyCall paycompletednotifycall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("第三方交易完成通知");
	}

	@Before
	public void caseUp() {

		paycompletednotifybean = new PayCompletedNotifyBean();
		paycompletednotifycall = new PayCompletedNotifyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(paycompletednotifycall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	//该接口即将废弃
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_PayCompletedNotify_001() {
		Logger.start(true,"第三方交易完成通知成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");		
			Map map = new OrderCallService().appSaveOrder();		
			Logger.comment("前置数据准备结束");
			int orderId = (Integer) map.get("orderId");
			int sellerId = (Integer) map.get("sellerId");
			int userId = (Integer) map.get("userId");
			int tradingId = (Integer) map.get("tradingId");
			DecimalFormat df = new DecimalFormat("0.00");// 保留两位小数
			BigDecimal totalPrice = (BigDecimal) map.get("totalPrice");// 支付金额
			// 支付前账户金额
			DefaultWapper database = new DefaultWapper();
			HashMap<String, Integer> m1 = new HashMap<String, Integer>();
			m1.put("UserId", userId);
			BigDecimal price1 = (BigDecimal) database.selectAccountInfo(m1).get(0).get("fAvailAmount");
			/*
			 * //支付 //根据订单号查询交易ID new
			 * OrderCallService().buyerOrderPay(TotalPrice, TradingId); //接单 new
			 * OrderCallService().acceptOrder(OrderId, SellerId); //卖家申请补款
			 * BigDecimal PayAmount = new BigDecimal(10); String PostPayId = new
			 * OrderCallService().ApplyForPostPay(OrderId, SellerId, PayAmount);
			 */
			// 准备参数
			int TradingId = new YmtTradingItemWapper().getTradingId(orderId);
			paycompletednotifybean.setBalanceAmount(totalPrice);
			paycompletednotifybean.setCurType("CNY");
			paycompletednotifybean.setTradingId(TradingId);
			// 发送请求
			paycompletednotifycall.setData(paycompletednotifybean);
			paycompletednotifycall.callService();
			Logger.verifyEquals(ECode.SUCCESS, paycompletednotifycall.getCode(), "验证Code");
			// Logger.verifyEquals(ErrorCode.SUCCESSMSG,
			// paycompletednotifycall.getMsg(), "验证Msg");
			// 数据库验证
			// 支付后金额
			BigDecimal price2 = (BigDecimal) database.selectAccountInfo(m1).get(0).get("fAvailAmount");
			HashMap<String, Integer> m = new HashMap<String, Integer>();
			m.put("OrderId", orderId);
			m.put("TradingStatus", 2);
			m.put("TraddingId", orderId);
			// m.put("PaidInFull", 1);
			m.put("SellerId", sellerId);
			// Logger.debug(OrderId);
			Logger.verifyEquals(1, database.selectOrder(m).size(), "验证订单表记录");
			Logger.verifyEquals(1, database.selectTradingStatus(m).size(), "验证交易表记录");
			Logger.verifyEquals(totalPrice, price1.subtract(price2), "验证账户金额");
			//Thread.sleep(500);
			//Logger.verifyEquals(1, database.selectTradingNotifaction(m).size(), "验证交易通知表");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}